package remote.controls
{
	import flash.events.MouseEvent;
	
	import remote.enums.HAlgin;
	import remote.enums.VAlgin;
	import remote.layouts.HorizontalLayout;
	import remote.skins.RadioButtonIconSkin;
	import remote.skins.SolidRectangleSkin;
	import remote.skins.brushes.SolidBrush;
	
	/**
	 * 单选按钮
	 * @createDate 2011-3-2
	 * @author 徐俊杰
	 */
	public class RadioButton extends Button
	{
		private var _icon:Button;
		
		private var _group:RadioButtonGroup;
		private var _value:Object;
		
		public function RadioButton()
		{
			super();
			this._toggle = true;
			this.updateToggle();
		}
		/**
		 * @inheritDoc 
		 * 
		 */	
		override public function setMouseStatus(value:String):void
		{
			if(this._mouseStatus != value)
				_icon.setMouseStatus(value);
			super.setMouseStatus(value);
		}
		/**
		 * @inheritDoc 
		 * 
		 */	
		override protected function createChildren():void
		{
			super.createChildren();
			_icon = new Button();
			_icon.setSize(10, 10);
			_icon.skin = new RadioButtonIconSkin();
			addChildAt(_icon, 0);
		}
		/**
		 * @inheritDoc 
		 * 
		 */	
		override protected function createSkin():void
		{
			_skin = new SolidRectangleSkin(new SolidBrush(0, 0, 0, 0, 0));
		}
		/**
		 * @inheritDoc 
		 * 
		 */	
		override protected function createLayout():void
		{
			_layout = new HorizontalLayout(this, 5, VAlgin.MIDDLE, HAlgin.CENTER);
		}
		/**
		 * @inheritDoc 
		 * 
		 */	
		override protected function clickHandler(e:MouseEvent):void
		{
			if(selected != true)
				selected = true;
		}
		/**
		 * @inheritDoc 
		 * 
		 */	
		override public function set selected(value:Boolean):void
		{
			if(group == null)
				this.setSelected(value);
			else if(group != null && this.selected != value)
			{
				if(value)
					group.selection = this;
				else
					group.selection = null
			}
		}

		public function get value():Object
		{
			return _value;
		}

		public function set value(value:Object):void
		{
			_value = value;
		}

		public function get group():RadioButtonGroup
		{
			return _group;
		}

		public function set group(value:RadioButtonGroup):void
		{
			if(_group != value)
			{
				if(_group != null)
					_group.removeRadioButton(this);
				value.addRadioButton(this);
			}
			
		}
	
		internal function setGroup(group:RadioButtonGroup):void
		{
			_group = group;
		}

		internal function setSelected(value:Boolean):void
		{
			super.selected = value;
			_icon.selected = value;
		}
		/**
		 * @inheritDoc 
		 * 
		 */	
		override public function set toggle(value:Boolean):void
		{
			throw new Error("RadioButton不能修改toggle属性");
		}
		public function get icon():Button
		{
			return _icon;
		}

	}
}